/*******************************************************************************
** Qt Advanced Docking System
** Copyright (C) 2017 Uwe Kindler
** 
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; either
** version 2.1 of the License, or (at your option) any later version.
** 
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
** Lesser General Public License for more details.
** 
** You should have received a copy of the GNU Lesser General Public
** License along with this library; If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/


//============================================================================
/// \file   DockWidgetTab.cpp
/// \author Uwe Kindler
/// \date   27.02.2017
/// \brief  Implementation of CDockWidgetTab class
//============================================================================


//============================================================================
//                                   INCLUDES
//============================================================================
#include <AutoHideDockContainer.h>
#include "FloatingDragPreview.h"
#include "ElidingLabel.h"
#include "DockWidgetTab.h"

#include <QBoxLayout>
#include <QLabel>
#include <QMouseEvent>
#include <QStyle>
#include <QApplication>
#include <QSplitter>
#include <QDebug>
#include <QToolButton>
#include <QPushButton>
#include <QMenu>

#include "ads_globals.h"
#include "DockWidget.h"
#include "DockAreaWidget.h"
#include "FloatingDockContainer.h"
#include "DockOverlay.h"
#include "DockManager.h"
#include "IconProvider.h"
#include "DockFocusController.h"


namespace ads
{
static const char* const LocationProperty = "Location";
using tTabLabel = CElidingLabel;

// 私有数据类CDockWidgetTab类（pimpl）
struct DockWidgetTabPrivate
{
    CDockWidgetTab* _this;
    CDockWidget* DockWidget;
    QLabel* IconLabel = nullptr;
    tTabLabel* TitleLabel;
    QPoint GlobalDragStartMousePosition;
    QPoint DragStartMousePosition;
    bool IsActiveTab = false;
    CDockAreaWidget* DockArea = nullptr;
    eDragState DragState = DraggingInactive;
    IFloatingWidget* FloatingWidget = nullptr;
    QIcon Icon;
    QAbstractButton* CloseButton = nullptr;
    QSpacerItem* IconTextSpacer;
    QPoint TabDragStartPosition;
    QSize IconSize;

    // 私有数据构造函数
    DockWidgetTabPrivate(CDockWidgetTab* _public);

    // 创建包含所有控件的完整布局
    void createLayout();

    // 根据鼠标事件中的位置移动选项卡
    void moveTab(QMouseEvent* ev);

    // 测试当前拖动状态的函数
    bool isDraggingState(eDragState dragState) const
    {
        return this->DragState == dragState;
    }

    // 开始浮动属于该标题栏的停靠窗口，如果由于任何原因无法浮动，则返回true
    bool startFloating(eDragState DraggingState = DraggingFloatingWidget);

    // 如果设置了给定的配置标志，则返回true
    bool testConfigFlag(CDockManager::eConfigFlag Flag) const
    {
        return CDockManager::testConfigFlag(Flag);
    }

    // 创建QPushButton或QToolButton作为关闭按钮
    QAbstractButton* createCloseButton() const
    {
        if (testConfigFlag(CDockManager::TabCloseButtonIsToolButton))
        {
            auto Button = new QToolButton();
            Button->setAutoRaise(true);
            return Button;
        }
        else
        {
            return new QPushButton();
        }
    }

    // 根据当前特性/配置更新关闭按钮的可见性
    void updateCloseButtonVisibility(bool active)
    {
        bool DockWidgetClosable = DockWidget->features().testFlag(CDockWidget::DockWidgetClosable);
        bool ActiveTabHasCloseButton = testConfigFlag(CDockManager::ActiveTabHasCloseButton);
        bool AllTabsHaveCloseButton = testConfigFlag(CDockManager::AllTabsHaveCloseButton);
        bool TabHasCloseButton = (ActiveTabHasCloseButton && active) | AllTabsHaveCloseButton;
        CloseButton->setVisible(DockWidgetClosable && TabHasCloseButton);
    }

    // 根据RetainTabSizeWhenCloseButtonHidden特性更新关闭按钮的大小策略
    void updateCloseButtonSizePolicy()
    {
        auto Features = DockWidget->features();
        auto SizePolicy = CloseButton->sizePolicy();
        SizePolicy.setRetainSizeWhenHidden(Features.testFlag(CDockWidget::DockWidgetClosable)
            && testConfigFlag(CDockManager::RetainTabSizeWhenCloseButtonHidden));
        CloseButton->setSizePolicy(SizePolicy);
    }

    template <typename T>
    IFloatingWidget* createFloatingWidget(T* Widget, bool CreateContainer)
    {
        if (CreateContainer)
        {
            return new CFloatingDockContainer(Widget);
        }
        else
        {
            auto w = new CFloatingDragPreview(Widget);
            _this->connect(w, &CFloatingDragPreview::draggingCanceled, [=]()
            {
                DragState = DraggingInactive;
            });
            return w;
        }
    }

    // 保存拖动开始时的全局和本地坐标
    void saveDragStartMousePosition(const QPoint& GlobalPos)
    {
        GlobalDragStartMousePosition = GlobalPos;
        DragStartMousePosition = _this->mapFromGlobal(GlobalPos);
    }

    // 如果图标大小改变，则更新图标
    void updateIcon()
    {
        if (!IconLabel || Icon.isNull())
        {
            return;
        }
        if (IconSize.isValid())
        {
            IconLabel->setPixmap(Icon.pixmap(IconSize));
        }
        else
        {
            IconLabel->setPixmap(Icon.pixmap(_this->style()->pixelMetric(QStyle::PM_SmallIconSize, nullptr, _this)));
        }
        IconLabel->setVisible(true);
    }

    // 方便访问停靠管理器的停靠焦点控制器
    CDockFocusController* focusController() const
    {
        return DockWidget->dockManager()->dockFocusController();
    }

    // 为“自动隐藏组到…”菜单创建和初始化菜单项的辅助函数
    QAction* createAutoHideToAction(const QString& Title, SideBarLocation Location, QMenu* Menu)
    {
        auto Action = Menu->addAction(Title);
        Action->setProperty("Location", Location);
        QObject::connect(Action, &QAction::triggered, _this, &CDockWidgetTab::onAutoHideToActionClicked);
        return Action;
    }
};
// 结构体DockWidgetTabPrivate


//============================================================================
// DockWidgetTabPrivate 构造函数
DockWidgetTabPrivate::DockWidgetTabPrivate(CDockWidgetTab* _public) :
    _this(_public)
{
}

//============================================================================
// 创建布局
void DockWidgetTabPrivate::createLayout()
{
    // 创建并设置标题标签的属性
    TitleLabel = new tTabLabel();
    TitleLabel->setElideMode(Qt::ElideRight);
    TitleLabel->setText(DockWidget->windowTitle());
    TitleLabel->setObjectName("dockWidgetTabLabel");
    TitleLabel->setAlignment(Qt::AlignCenter);
    _this->connect(TitleLabel, SIGNAL(elidedChanged(bool)), SIGNAL(elidedChanged(bool)));

    // 创建并设置关闭按钮的属性
    CloseButton = createCloseButton();
    CloseButton->setObjectName("tabCloseButton");
    internal::setButtonIcon(CloseButton, QStyle::SP_TitleBarCloseButton, TabCloseIcon);
    CloseButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    CloseButton->setFocusPolicy(Qt::NoFocus);
    internal::setToolTip(CloseButton, QObject::tr("关闭标签"));
    _this->connect(CloseButton, SIGNAL(clicked()), SIGNAL(closeRequested()));

    // 计算布局的间距
    QFontMetrics fm(TitleLabel->font());
    int Spacing = qRound(fm.height() / 4.0);

    // 创建布局并添加小部件
    QBoxLayout* Layout = new QBoxLayout(QBoxLayout::LeftToRight);
    Layout->setContentsMargins(2 * Spacing, 0, 0, 0);
    Layout->setSpacing(0);
    _this->setLayout(Layout);
    Layout->addWidget(TitleLabel, 1);
    Layout->addSpacing(Spacing);
    Layout->addWidget(CloseButton);
    Layout->addSpacing(qRound(Spacing * 4.0 / 3.0));
    Layout->setAlignment(Qt::AlignCenter);

    // 设置标题标签可见
    TitleLabel->setVisible(true);
}

//============================================================================
// 移动标签
void DockWidgetTabPrivate::moveTab(QMouseEvent* ev)
{
    ev->accept();
    QPoint Distance = internal::globalPositionOf(ev) - GlobalDragStartMousePosition;
    Distance.setY(0);
    auto TargetPos = Distance + TabDragStartPosition;
    TargetPos.rx() = qMax(TargetPos.x(), 0);
    TargetPos.rx() = qMin(_this->parentWidget()->rect().right() - _this->width() + 1, TargetPos.rx());
    _this->move(TargetPos);
    _this->raise();
}

//============================================================================
// 开始浮动标签
bool DockWidgetTabPrivate::startFloating(eDragState DraggingState)
{
    auto dockContainer = DockWidget->dockContainer();
    ADS_PRINT("isFloating " << dockContainer->isFloating());
    ADS_PRINT("areaCount " << dockContainer->dockAreaCount());
    ADS_PRINT("widgetCount " << DockWidget->dockAreaWidget()->dockWidgetsCount());

    // 如果该浮动窗口中只有一个标签，不再进行浮动
    if (dockContainer->isFloating()
        && (dockContainer->visibleDockAreaCount() == 1)
        && (DockWidget->dockAreaWidget()->dockWidgetsCount() == 1))
    {
        return false;
    }

    ADS_PRINT("startFloating");
    DragState = DraggingState;
    IFloatingWidget* FloatingWidget = nullptr;
    bool CreateContainer = (DraggingFloatingWidget != DraggingState);

    // 根据标签数量创建浮动窗口
    QSize Size;
    if (DockArea->dockWidgetsCount() > 1)
    {
        FloatingWidget = createFloatingWidget(DockWidget, CreateContainer);
        Size = DockWidget->size();
    }
    else
    {
        FloatingWidget = createFloatingWidget(DockArea, CreateContainer);
        Size = DockArea->size();
    }

    if (DraggingFloatingWidget == DraggingState)
    {
        FloatingWidget->startFloating(DragStartMousePosition, Size, DraggingFloatingWidget, _this);
        auto DockManager = DockWidget->dockManager();
        auto Overlay = DockManager->containerOverlay();
        Overlay->setAllowedAreas(OuterDockAreas);
        this->FloatingWidget = FloatingWidget;
        qApp->postEvent(DockWidget, new QEvent((QEvent::Type)internal::DockedWidgetDragStartEvent));
    }
    else
    {
        FloatingWidget->startFloating(DragStartMousePosition, Size, DraggingInactive, nullptr);
    }

    return true;
}


//============================================================================
// CDockWidgetTab 构造函数
// 参数：
//   DockWidget - 指向CDockWidget的指针
//   parent - 父窗口指针
CDockWidgetTab::CDockWidgetTab(CDockWidget* DockWidget, QWidget *parent) :
    QFrame(parent),
    d(new DockWidgetTabPrivate(this))
{
    // 设置属性
    setAttribute(Qt::WA_NoMousePropagation, true);
    // 设置私有成员变量
    d->DockWidget = DockWidget;
    // 创建布局
    d->createLayout();
    // 设置焦点策略
    setFocusPolicy(Qt::NoFocus);
}
//============================================================================
// CDockWidgetTab 析构函数
CDockWidgetTab::~CDockWidgetTab()
{
    ADS_PRINT("~CDockWidgetTab()");
    delete d;
}
//============================================================================
// 鼠标按下事件
// 参数：
//   ev - 鼠标事件指针
void CDockWidgetTab::mousePressEvent(QMouseEvent* ev)
{
    if (ev->button() == Qt::LeftButton)
    {
        ev->accept();
        // 保存拖动起始位置
        d->saveDragStartMousePosition(internal::globalPositionOf(ev));
        // 设置拖动状态为 DraggingMousePressed
        d->DragState = DraggingMousePressed;
        // 如果启用焦点高亮功能
        if (CDockManager::testConfigFlag(CDockManager::FocusHighlighting))
        {
            // 设置 DockWidgetTab 按下状态为 true
            d->focusController()->setDockWidgetTabPressed(true);
            // 设置 DockWidgetTab 焦点为当前 DockWidgetTab
            d->focusController()->setDockWidgetTabFocused(this);
        }
        // 发送 clicked 信号
        Q_EMIT clicked();
        return;
    }
    // 调用父类的鼠标按下事件处理函数
    Super::mousePressEvent(ev);
}
//============================================================================
// 鼠标释放事件
// 参数：
//   ev - 鼠标事件指针
void CDockWidgetTab::mouseReleaseEvent(QMouseEvent* ev)
{
    if (ev->button() == Qt::LeftButton)
    {
        // 获取当前拖动状态
        auto CurrentDragState = d->DragState;
        // 清空全局拖动起始位置和拖动起始位置
        d->GlobalDragStartMousePosition = QPoint();
        d->DragStartMousePosition = QPoint();
        // 设置拖动状态为 DraggingInactive
        d->DragState = DraggingInactive;
        switch (CurrentDragState)
        {
        case DraggingTab:
            // 如果是标签拖动结束，发送 moved 信号
            if (d->DockArea)
            {
                ev->accept();
                Q_EMIT moved(internal::globalPositionOf(ev));
            }
            break;
        case DraggingFloatingWidget:
            ev->accept();
            // 结束浮动窗口拖动
            d->FloatingWidget->finishDragging();
            break;
        default:
            // 如果启用焦点高亮功能，设置 DockWidgetTab 按下状态为 false
            if (CDockManager::testConfigFlag(CDockManager::FocusHighlighting))
            {
                d->focusController()->setDockWidgetTabPressed(false);
            }
            break; // 什么都不做
        }
    }
    else if (ev->button() == Qt::MiddleButton)
    {
        // 如果启用中键关闭标签功能，并且 DockWidget 是可关闭的
        if (CDockManager::testConfigFlag(CDockManager::MiddleMouseButtonClosesTab) && d->DockWidget->features().testFlag(CDockWidget::DockWidgetClosable))
        {
            // 只有鼠标仍在窗口上方时才尝试关闭，以允许用户取消操作
            if (rect().contains(mapFromGlobal(QCursor::pos()))) {
                ev->accept();
                // 发送 closeRequested 信号
                Q_EMIT closeRequested();
            }
        }
    }
    // 调用父类的鼠标释放事件处理函数
    Super::mouseReleaseEvent(ev);
}


//============================================================================
void CDockWidgetTab::mouseMoveEvent(QMouseEvent* ev)
{
    if (!(ev->buttons() & Qt::LeftButton) || d->isDraggingState(DraggingInactive))
    {
        // 如果鼠标左键未按下或者dock widget处于非活动拖动状态
        d->DragState = DraggingInactive;
        Super::mouseMoveEvent(ev);
        return;
    }

    // 移动浮动窗口
    if (d->isDraggingState(DraggingFloatingWidget))
    {
        d->FloatingWidget->moveFloating();
        Super::mouseMoveEvent(ev);
        return;
    }

    // 移动标签
    if (d->isDraggingState(DraggingTab))
    {
        // 移动标签总是允许的，因为它不涉及移动dock widget
        d->moveTab(ev);
    }

    auto MappedPos = mapToParent(ev->pos());
    bool MouseOutsideBar = (MappedPos.x() < 0) || (MappedPos.x() > parentWidget()->rect().right());
    // 或许固定的拖动距离更好？
    int DragDistanceY = qAbs(d->GlobalDragStartMousePosition.y() - internal::globalPositionOf(ev).y());
    if (DragDistanceY >= CDockManager::startDragDistance() || MouseOutsideBar)
    {
        // 如果是dock container中最后一个dock area且只有一个dock widget，将其移动到新的浮动窗口并留下空的dock container是没有意义的
        if (d->DockArea->dockContainer()->isFloating()
            && d->DockArea->openDockWidgetsCount() == 1
            && d->DockArea->dockContainer()->visibleDockAreaCount() == 1)
        {
            return;
        }
        // 只有可浮动的widget才允许浮动
        auto Features = d->DockWidget->features();
        if (Features.testFlag(CDockWidget::DockWidgetFloatable) || (Features.testFlag(CDockWidget::DockWidgetMovable)))
        {
            // 如果我们拖出去了，需要恢复标签的初始位置，否则会看起来很奇怪
            if (d->isDraggingState(DraggingTab))
            {
                parentWidget()->layout()->update();
            }
            d->startFloating();
        }
        return;
    }
    else if (d->DockArea->openDockWidgetsCount() > 1
        && (internal::globalPositionOf(ev) - d->GlobalDragStartMousePosition).manhattanLength() >= QApplication::startDragDistance()) // 等待几个像素再开始移动
    {
        // 如果我们开始拖动标签，保存其初始位置以便稍后恢复
        if (DraggingTab != d->DragState)
        {
            d->TabDragStartPosition = this->pos();
        }
        d->DragState = DraggingTab;
        return;
    }
    Super::mouseMoveEvent(ev);
}

//============================================================================

void CDockWidgetTab::contextMenuEvent(QContextMenuEvent* ev)
{
    ev->accept();
    if (d->isDraggingState(DraggingFloatingWidget))
    {
        return;
    }
    d->saveDragStartMousePosition(ev->globalPos());

    const bool isFloatable = d->DockWidget->features().testFlag(CDockWidget::DockWidgetFloatable);
    const bool isNotOnlyTabInContainer = !d->DockArea->dockContainer()->hasTopLevelDockWidget();
    const bool isTopLevelArea = d->DockArea->isTopLevelArea();
    const bool isDetachable = isFloatable && isNotOnlyTabInContainer;
    QAction* Action;
    QMenu Menu(this);
    if (!isTopLevelArea)
    {
        Action = Menu.addAction(tr("分离"), this, SLOT(detachDockWidget()));
        Action->setEnabled(isDetachable);
        if (CDockManager::testAutoHideConfigFlag(CDockManager::AutoHideFeatureEnabled))
        {
            Action = Menu.addAction(tr("固定"), this, SLOT(autoHideDockWidget()));
            auto IsPinnable = d->DockWidget->features().testFlag(CDockWidget::DockWidgetPinnable);
            Action->setEnabled(IsPinnable);
            auto menu = Menu.addMenu(tr("固定到..."));
            menu->setEnabled(IsPinnable);
            d->createAutoHideToAction(tr("顶部"), SideBarTop, menu);
            d->createAutoHideToAction(tr("左侧"), SideBarLeft, menu);
            d->createAutoHideToAction(tr("右侧"), SideBarRight, menu);
            d->createAutoHideToAction(tr("底部"), SideBarBottom, menu);
        }
    }
    Menu.addSeparator();
    Action = Menu.addAction(tr("关闭"), this, SIGNAL(closeRequested()));
    Action->setEnabled(isClosable());
    if (d->DockArea->openDockWidgetsCount() > 1)
    {
        Action = Menu.addAction(tr("关闭其他"), this, SIGNAL(closeOtherTabsRequested()));
    }
    Menu.exec(ev->globalPos());
}


//============================================================================
bool CDockWidgetTab::isActiveTab() const
{
    return d->IsActiveTab;
}
//============================================================================
void CDockWidgetTab::setActiveTab(bool active)
{
    d->updateCloseButtonVisibility(active);

    // 关于焦点的相关操作
    if (CDockManager::testConfigFlag(CDockManager::FocusHighlighting) && !d->DockWidget->dockManager()->isRestoringState())
    {
        bool updateFocusStyle = false;
        if (active && !hasFocus())
        {
            //setFocus(Qt::OtherFocusReason);
            d->focusController()->setDockWidgetTabFocused(this);
            updateFocusStyle = true;
        }
        if (d->IsActiveTab == active)
        {
            if (updateFocusStyle)
            {
                updateStyle();
            }
            return;
        }
    }
    else if (d->IsActiveTab == active)
    {
        return;
    }
    d->IsActiveTab = active;
    updateStyle();
    update();
    updateGeometry();
    Q_EMIT activeTabChanged();
}
//============================================================================
CDockWidget* CDockWidgetTab::dockWidget() const
{
    return d->DockWidget;
}
//============================================================================
void CDockWidgetTab::setDockAreaWidget(CDockAreaWidget* DockArea)
{
    d->DockArea = DockArea;
}
//============================================================================
CDockAreaWidget* CDockWidgetTab::dockAreaWidget() const
{
    return d->DockArea;
}
//============================================================================
void CDockWidgetTab::setIcon(const QIcon& Icon)
{
    QBoxLayout* layout = qobject_cast<QBoxLayout*>(layout());
    if (!d->IconLabel && Icon.isNull())
    {
        return;
    }
    if (!d->IconLabel)
    {
        d->IconLabel = new QLabel();
        d->IconLabel->setAlignment(Qt::AlignVCenter);
        d->IconLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
        internal::setToolTip(d->IconLabel, d->TitleLabel->toolTip());
        layout->insertWidget(0, d->IconLabel, Qt::AlignVCenter);
        layout->insertSpacing(1, qRound(1.5 * layout->contentsMargins().left() / 2.0));
    }
    else if (Icon.isNull())
    {
        // 移除图标标签和间隔项
        layout->removeWidget(d->IconLabel);
        layout->removeItem(layout->itemAt(0));
        delete d->IconLabel;
        d->IconLabel = nullptr;
    }
    d->Icon = Icon;
    d->updateIcon();
}


//============================================================================
// 获取dock widget标签的图标
const QIcon& CDockWidgetTab::icon() const
{
	return d->Icon;
}
//============================================================================
// 获取dock widget标签的文本
QString CDockWidgetTab::text() const
{
	return d->TitleLabel->text();
}
//============================================================================
// 处理鼠标双击事件
void CDockWidgetTab::mouseDoubleClickEvent(QMouseEvent *event)
{
	if (event->button() == Qt::LeftButton)
	{
		// 如果需要将dock widget移动到新的浮动窗口并使当前窗口为空，则执行以下操作
		if ((!d->DockArea->dockContainer()->isFloating() || d->DockArea->dockWidgetsCount() > 1)
			&& d->DockWidget->features().testFlag(CDockWidget::DockWidgetFloatable))
		{
			event->accept();
			d->saveDragStartMousePosition(internal::globalPositionOf(event));
			d->startFloating(DraggingInactive);
		}
	}
	Super::mouseDoubleClickEvent(event);
}
//============================================================================
// 设置dock widget标签的可见性
void CDockWidgetTab::setVisible(bool visible)
{
	visible &= !d->DockWidget->features().testFlag(CDockWidget::NoTab);
	Super::setVisible(visible);
}
//============================================================================
// 设置dock widget标签的文本
void CDockWidgetTab::setText(const QString& title)
{
	d->TitleLabel->setText(title);
}
//============================================================================
// 检查dock widget标签的标题是否被省略
bool CDockWidgetTab::isTitleElided() const
{
	return d->TitleLabel->isElided();
}
//============================================================================
// 检查dock widget标签是否可关闭
bool CDockWidgetTab::isClosable() const
{
	return d->DockWidget && d->DockWidget->features().testFlag(CDockWidget::DockWidgetClosable);
}
//===========================================================================
// 分离dock widget并开始浮动
void CDockWidgetTab::detachDockWidget()
{
	if (!d->DockWidget->features().testFlag(CDockWidget::DockWidgetFloatable))
	{
		return;
	}
	d->saveDragStartMousePosition(QCursor::pos());
	d->startFloating(DraggingInactive);
}
//===========================================================================
// 设置dock widget为自动隐藏
void CDockWidgetTab::autoHideDockWidget()
{
	d->DockWidget->setAutoHide(true);
}


//===========================================================================
void CDockWidgetTab::onAutoHideToActionClicked()
{
    // 获取 sender() 的 LocationProperty 属性并转换为整数
    int Location = sender()->property(LocationProperty).toInt();
    
    // 调用 DockWidget 的 toggleAutoHide 方法，传入 Location 参数
    d->DockWidget->toggleAutoHide((SideBarLocation)Location);
}
//============================================================================
bool CDockWidgetTab::event(QEvent *e)
{
#ifndef QT_NO_TOOLTIP
    // 如果事件类型是 ToolTipChange
    if (e->type() == QEvent::ToolTipChange)
    {
        // 获取工具提示文本
        const auto text = toolTip();
        
        // 设置 TitleLabel 的工具提示为 text
        d->TitleLabel->setToolTip(text);
        
        // 如果 IconLabel 存在，则设置其工具提示为 text
        if (d->IconLabel) {
            d->IconLabel->setToolTip(text);
        }
    }
#endif
    
    // 如果事件类型是 StyleChange
    if (e->type() == QEvent::StyleChange)
    {
        // 更新图标
        d->updateIcon();
    }
    
    // 调用父类的 event 方法，并返回结果
    return Super::event(e);
}
//============================================================================
void CDockWidgetTab::onDockWidgetFeaturesChanged()
{
    // 更新 CloseButton 的大小策略
    d->updateCloseButtonSizePolicy();
    
    // 更新 CloseButton 的可见性，传入 isActiveTab() 的结果
    d->updateCloseButtonVisibility(isActiveTab());
}
//============================================================================
void CDockWidgetTab::setElideMode(Qt::TextElideMode mode)
{
    // 设置 TitleLabel 的省略模式为 mode
    d->TitleLabel->setElideMode(mode);
}
//============================================================================
void CDockWidgetTab::updateStyle()
{
    // 重新应用样式
    internal::repolishStyle(this, internal::RepolishDirectChildren);
}
//============================================================================
QSize CDockWidgetTab::iconSize() const
{
    // 返回 IconSize
    return d->IconSize;
}
//============================================================================
void CDockWidgetTab::setIconSize(const QSize& Size)
{
    // 设置 IconSize 为 Size
    d->IconSize = Size;
    
    // 更新图标
    d->updateIcon();
}

} // namespace ads
//---------------------------------------------------------------------------
// EOF DockWidgetTab.cpp
